package com.brainsz.limiter;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;

import android.preference.PreferenceManager;
import android.provider.CallLog;
import android.provider.CallLog.Calls;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;

public class Calllimiter extends Activity {
    /** Called when the activity is first created. */
	SharedPreferences preferences;
	
	private static final String TAG = "MyActivity";
	
	
	
	private ProgressBar smsProgressBar;
	private ProgressBar minutesProgressBar;
	
	int myProgress = 0;
	  
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        smsProgressBar = (ProgressBar)findViewById(R.id.smsBar);
        minutesProgressBar = (ProgressBar)findViewById(R.id.minutesBar);
    }
        

    public void onClickHandler(View view)
    {
    
    	switch (view.getId()) {
			
    	case R.id.show_button:
    		String minutes = preferences.getString("minutes", "n/a");
    		String sms = preferences.getString("sms", "n/a");
    		
			showPrefs(minutes, sms);
			
			break;
			
    	case R.id.call_button:
    		
    		String minutes_actual = null;
    		String sms_actual = null;
    		Log.v(TAG, "not passed");
    		int duration = getCallDuration();
    		Log.v(TAG, "passed");
    		showCallDuration(duration); 
    		
    		break;
    	
		}
    }
    // http://www.dcpagesapps.com/developer-resources/android/25-android-tutorial-call-logs
    private int getCallDuration()
    {
    
    	String[] strFields = {
    	        android.provider.CallLog.Calls.NUMBER, 
    	        android.provider.CallLog.Calls.TYPE,
    	        android.provider.CallLog.Calls.CACHED_NAME,
    	        android.provider.CallLog.Calls.CACHED_NUMBER_TYPE
    	        };
    	
    	

    	Cursor mCallCursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, strFields, null, null, CallLog.Calls.DATE + " DESC");
    	int duration = 0;
    	if(mCallCursor.moveToFirst()){
    		 
    		  // loop through cursor 
    		  
    			  int durationColumn = mCallCursor.getColumnIndex(CallLog.Calls.DURATION);
    		    	duration = Integer.parseInt(mCallCursor.getString(durationColumn));
    		    	Log.v(TAG, Integer.toString(duration));
    		    	return duration;
    		 
    		  } else { 
    		 
    		}

    	return duration;

    }
    
    
    private void showPrefs(String minutes, String sms){
		Toast.makeText(this,"Input: " + minutes + " and: "+ sms, Toast.LENGTH_LONG).show();

	}
    
    private void showCallDuration(int duration)
    {
    	Toast.makeText(this, "Duration: " + duration, Toast.LENGTH_LONG).show();
    }
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.preferencesmenu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		
		switch (item.getItemId()) {
		// We have only one menu option
		case R.id.preferences:
			Intent i = new Intent(this, MyPreference.class);
			startActivity(i);
			break;

		}
		return true;

	}

}